package coms.delegates;

import EJB.EJBLocator;
import coms.dto.ConferenceItem;
import coms.dto.Publication;
import coms.ejb.ConferenceItemBeanRemote;
import coms.ejb.PublicationBeanRemote;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.*;

public class ConferenceItemDelegate implements IDelegate<ConferenceItem> {

    private ConferenceItemBeanRemote conferenceItemBean;
    private PublicationBeanRemote publicationBean;
    private Queue queue;
    private QueueConnectionFactory factory;
    private QueueConnection connection;
    private QueueSession session;
    private QueueSender sender;
    private String emailAddress, endDate, reviewStatus, reviewResult;
    private MapMessage msg;

    public ConferenceItemDelegate() {
        emailAddress = "";
        endDate = "";
        reviewStatus = "pending";
        reviewResult = "not available";
        try {
            this.conferenceItemBean = EJBLocator.lookupConferenceItemBean();
            this.publicationBean = (PublicationBeanRemote) EJBLocator.lookupPublicationBean();
            this.queue = (Queue) EJBLocator.lookupEmailMDB();
            this.factory = (QueueConnectionFactory) EJBLocator.lookupEmailFactory();
            this.connection = (QueueConnection) factory.createQueueConnection();
            this.session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
            this.sender = session.createSender(queue);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceItemDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public boolean create(ConferenceItem conferenceItem) {
        conferenceItemBean.create(conferenceItem);
        return true;
    }

    @Override
    public boolean delete(ConferenceItem conferenceItem) {
        boolean deleted = conferenceItemBean.delete(conferenceItem);
        return deleted;
    }

    @Override
    public boolean update(ConferenceItem conferenceItem) {
        conferenceItemBean.update(conferenceItem);
        return true;
    }

    @Override
    public List<ConferenceItem> search(ConferenceItem conferenceItem) throws Exception {
        return conferenceItemBean.search(conferenceItem);
    }

    @Override
    public List<ConferenceItem> findAll() throws Exception {
        return conferenceItemBean.findAll();
    }

    @Override
    public ConferenceItem getById(int id) {
        return conferenceItemBean.getById(id);
    }

    public void notifyAuthors(ConferenceItem conferenceItem) throws JMSException {
        List<Publication> publicationList = conferenceItem.getPublications();
        if (publicationList.isEmpty()) {
            return;
        }

        msg = session.createMapMessage();

        for (int i = 0; i < publicationList.size(); i++) {
            //get first author email
            emailAddress = publicationList.get(i).getFirstAuthor().getEmailAddress() + ",";
           /* for (int j = 0; j < publicationList.get(i).getCoAuthor().size(); j++) {
                //get all each co-authors email
                emailAddress += publicationList.get(j).getCoAuthor().get(j).getEmailAddress() + ",";
            }*/

            //waiting for Atul to complete publication status flag and publication result flag
            switch(publicationList.get(i).getStatus()){
                case -1:
                    reviewStatus = "REJECTED";
                    break;
                case 0:
                    reviewStatus = "PENDING";
                    break;
                case 1:
                    reviewStatus = "ACCEPTED";
                    break;
            }
            
            reviewResult = ""+publicationList.get(i).getRating();
            

            msg.setString("recipient", emailAddress);
            msg.setString("message", "Dear Author, \nYour publication review status is " + reviewStatus + " and the result is " + reviewResult);
            //finally client send messages asynchronously to the queue
            System.out.println("*******************Email Created");
            sender.send(msg);
        }


    }

    public void sendReminder(ConferenceItem conferenceItem) throws JMSException {
        List<Publication> publicationList = conferenceItem.getPublications();
        if (publicationList.isEmpty()) {
            System.out.println("pulication empty");
            return;
        }
        System.out.println("*********Size of List is " + publicationList.size());
        msg = session.createMapMessage();

        for(int i=0;i<publicationList.size();i++){
        //emailAddresses += publicationList.get(i).getFirstAuthor().getEmailAddress() + ",";
        emailAddress = publicationList.get(i).getFirstAuthor().getEmailAddress();
        endDate = conferenceItem.getEndDate().toString();


        msg.setString("recipient", emailAddress);
        msg.setString("message", "Dear Author, \nPlease be reminded that the deadline for publication submission is " + endDate);
        //finally client send messages asynchronously to the queue
        System.out.println("*******************Email Created");
        sender.send(msg);
        }
    }

    public void calculateReview(ConferenceItem conferenceItem) {
        //publicationBean. Atul please fill this up
        try {
            PublicationBeanRemote bean = EJBLocator.lookupPublicationBean();
            bean.processConferenceItemPublicationReviews(conferenceItem);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("ConferenceItemDelegate - calculateReview");
        }

    }
}
